<?php

namespace app\admin\controller\content;

use app\admin\controller\Controller;
use app\admin\model\Block as BlockModel;

/**
 * 内容块管理器
 * Class block
 * @package app\admin\controller\content
 */
class Block extends Controller
{
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function initialize()
    {
        parent::initialize();
        // 控制器初始化
        $this->model = new BlockModel();
    }
    /**
     * 内容块列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        return $this->renderSuccess($this->model->getList());
    }

    /**
     * 内容块分类
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function category()
    {
        return $this->renderSuccess($this->model->getCategory());
    }

    /**
     * 添加内容块
     * @return array|mixed
     */
    public function add()
    {
        // 新增记录
        if ($this->model->add($this->request->only(['block_tag','block_type', 'content','mark']))) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError($this->model->getError() ?? '添加失败');
    }

    /**
     * 内容块详细
     * @param $free_block_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 更新记录
        if ($data = $this->model->detail($this->request->param('free_block_id'))) {
            return $this->renderSuccess($data);
        }
        return $this->renderError($this->model->getError() ?? '更新失败');
    }

    /**
     * 编辑内容块
     * @param $free_block_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        // 更新记录
        if ($this->model->edit($this->request->only(['free_block_id','block_tag','block_type', 'content','mark']))) {
            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError($this->model->getError() ?? '更新失败');
    }

    /**
     * 删除内容块
     * @param $free_block_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        if (!$this->model->setDelete($this->request->param('free_block_id'))) {
            return $this->renderError($this->model->getError() ?? '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}